class AddOrganPositionStandard < Mutations::Command
  include Wisper::Publisher

  required do
    integer :position_id
    string  :organization_category
  end

  def execute
    unless organ_category = Organization::Category.find_by_name(organization_category)
      add_error(:organization_category, :organization_category_not_exist, '机构类型不存在')
      return
    end

    unless position = Position.find_by_id(position_id)
      add_error(:position_id, :position_not_exist, '岗位不存在')
      return
    end

    organ_category_position_standard = OrganCategoryPositionStandard.new
    organ_category_position_standard.position = position
    organ_category_position_standard.organization_category = organ_category

    if organ_category_position_standard.save
      return organ_category_position_standard
    else
      organ_category_position_standard.errors.each do |key, value|
        add_error(key, key, value)
      end
    end
  end
  
end